package Pipe;

import java.io.File;
import java.util.Scanner;

/**
 * 入口
 * @author fengke
 */
public class Main {
    public static void main(String[] args) {
        //第一批处理数据
        File infile = new File("/Users/fengke/Downloads/学习资料/软件体系结构/Pipe/input.txt");
        File outfile = new File("/Users/fengke/Downloads/学习资料/软件体系结构/Pipe/output.txt");
        Scanner inputFile;
        Scanner outputFile;

        //生成文件后数据 实验步骤7
        File inFile2 = new File("/Users/fengke/Downloads/学习资料/软件体系结构/Pipe/input2.txt");
        File outFile2 = new File("/Users/fengke/Downloads/学习资料/软件体系结构/Pipe/output2.txt");
        Scanner inputFile2;
        Scanner outputFile2;

        //处理input
        try {
            inputFile = new Scanner(infile);
            outputFile = new Scanner(outfile);
            // 3个管道 定义之后就已经在构造器将该管道写入器连接到接收器
            Pipe pipe1 = new Pipe();
            Pipe pipe2 = new Pipe();
            Pipe pipe3 = new Pipe();
            // 定义四种过滤器：读文件、单词移位、排序、写文件 对应实验相关步骤3、4、5、6
            Input input = new Input(infile, pipe1);
            Shift shift = new Shift(pipe1, pipe2);
            Alphabetizer alph = new Alphabetizer(pipe2, pipe3);
            Output output = new Output(pipe3, outfile);
            // 顺序执行四个过滤器
            input.transform();
            shift.transform();
            alph.transform();
            output.transform();
            // 直接输出结果
            System.out.println("-----  infile   -----");
            String str = null;
            while (inputFile.hasNextLine()) {
                str = inputFile.nextLine();
                System.out.println(str);
            }
            System.out.println("input  end");
            System.out.println("-----  outfile  -----");
            while (outputFile.hasNextLine()) {
                str = outputFile.nextLine();
                System.out.println(str);
            }
            inputFile.close();
            outputFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("-----------------------------------");
        //处理input2
        try {
            inputFile = new Scanner(inFile2);
            outputFile = new Scanner(outFile2);
            // 4个管道 定义之后就已经在构造器将该管道写入器连接到接收器
            Pipe pipe = new Pipe();
            Pipe pipe1 = new Pipe();
            Pipe pipe2 = new Pipe();
            Pipe pipe3 = new Pipe();
            // 定义四种过滤器：读文件、单词移位、排序、写文件 对应实验相关步骤3、4、5、6
            AddLine addLine = new AddLine(infile, pipe);
            Output fileOutPut = new Output(pipe, inFile2);
            Input input = new Input(inFile2, pipe1);
            Shift shift = new Shift(pipe1, pipe2);
            Alphabetizer alph = new Alphabetizer(pipe2, pipe3);
            Output output = new Output(pipe3, outFile2);
            // 顺序执行过滤器(完成step7)
            addLine.transform();
            fileOutPut.transform();
            input.transform();
            shift.transform();
            alph.transform();
            output.transform();
            // 直接输出结果
            System.out.println("-----  infile   -----");
            String str = null;
            while (inputFile.hasNextLine()) {
                str = inputFile.nextLine();
                System.out.println(str);
            }
            System.out.println("input  end");
            System.out.println("-----  outfile  -----");
            while (outputFile.hasNextLine()) {
                str = outputFile.nextLine();
                System.out.println(str);
            }
            inputFile.close();
            outputFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
